<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 95%;overflow-y: hidden">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="角色名称">
          <a-input placeholder="请输入岗位名称" v-decorator="[ 'jobName', validatorRules.jobName]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="所属部门" hasFeedback>
          <a-tree-select
            style="width:100%"
            :dropdownStyle="{maxHeight:'200px',overflow:'auto'}"
            :treeData="departTree"
            v-model="model.deptId"
            placeholder="请选择部门">
          </a-tree-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排序">
          <a-input-number placeholder="请输入排序" :min="0" style="width: 200px" v-decorator="[ 'sort',validatorRules.sort]"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { addJob, updateJob } from '@/api/job'
  import { queryDepartTreeList } from '@/api/dept'



  export default {
    name: 'RoleModal',
    data () {
      return {
        title: '操作',
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          jobName: {
            rules: [
              { required: true, message: '请输入岗位名称' },
              { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
            ]
          },
          sort: { initialValue: 0 }
        },

        departTree: [],
      }
    },
    created () {

    },
    methods: {
      loadDeptTreeData () {
        let that = this
        queryDepartTreeList().then((res) => {
          if (res.data) {
            that.departTree = res.data
          }

        })
      },
      add () {
        this.edit({})
      },
      edit (record) {
        this.form.resetFields()
        this.visible = true
        if (Object.keys(record).length != 0) {
          this.model = Object.assign({}, record)
          this.model.deptId = String(record.deptId)
          //编辑页面禁止修改角色编码
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.model, 'jobName','sort'))
          })
        } else {
          this.model.deptId = ''
        }
        this.loadDeptTreeData()


      },
      close () {
        this.$emit('close')
        this.dsType = 0;
        this.visible = false
      },
      handleOk () {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let formData = Object.assign(this.model, values)

            console.log(formData)
            let obj
            if (!this.model.id) {
              obj = addJob(formData)
            } else {
              obj = updateJob(formData)
            }
            obj.then((res) => {
              if (res.code == 200) {
                that.$message.success(res.msg)
                that.$emit('ok')
              } else {
                that.$message.warning(res.msg)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }
        })
      },
      handleCancel () {
        this.close()
      }
    }
  }
</script>

<style scoped>

</style>